package com.dhcc.bpm.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.common.exception.BusinessException;
import com.dhcc.bpm.modules.orga.entity.Orga;
import com.dhcc.bpm.modules.orga.service.IOrgaService;
import com.dhcc.bpm.modules.system.entity.Dept;
import com.dhcc.bpm.modules.system.mapper.DeptMapper;
import com.dhcc.bpm.modules.system.service.IDeptService;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import com.dhcc.bpm.modules.system.vo.DeptVO;
import com.dhcc.bpm.modules.system.vo.DeptVO2;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName DeptServiceImpl
 * @Description  服务实现类
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@Service
@AllArgsConstructor
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

	private IOrgaService orgaService;

	private ISerialNumberService serialNumberService;

	private StringRedisTemplate stringRedisTemplate;
	private static final String EXP_SQE = "expSeq";

	/**
	 * 自定义分页
	 * @Author sjm
	 * @Date 19:09 2020/2/21 0021
	 * @param page
	 * @param dept
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.DeptVO>
	 **/
	@Override
	public IPage<DeptVO> selectDeptPage(IPage<DeptVO> page, DeptVO dept) {
		return page.setRecords(baseMapper.selectDeptPage(page, dept));
	}

	/**
	 * 树形结构
	 * @Author sjm
	 * @Date 19:09 2020/2/21 0021
	 * @param tenantId
	 * @return java.util.List<com.dhcc.bpm.modules.system.vo.DeptVO>
	 **/
	@Override
	public List<DeptVO> tree(String tenantId) {
		return ForestNodeMerger.merge(baseMapper.tree(tenantId));
	}

	/**
	 * 获取部门名
	 * @Author sjm
	 * @Date 19:09 2020/2/21 0021
	 * @param deptIds
	 * @return java.util.List<java.lang.String>
	 **/
	@Override
	public List<String> getDeptNames(String deptIds) {
		return baseMapper.getDeptNames(Func.toStrArray(deptIds));
	}

	/**
	 * 删除部门
	 * @Author sjm
	 * @Date 19:09 2020/2/21 0021
	 * @param ids
	 * @return boolean
	 **/
	@Override
	public boolean removeDept(String ids) {
		Integer cnt = Math.toIntExact(baseMapper.selectCount(Wrappers.<Dept>query().lambda().in(Dept::getParentId, Func.toLongList(ids))));
		if (cnt > 0) {
			throw new BusinessException("请先删除子节点!");
		}
		return baseMapper.removeByIds(ids.split(","));
	}

	/**
	 * 提交
	 * @Author sjm
	 * @Date 19:09 2020/2/21 0021
	 * @param dept
	 * @return boolean
	 **/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean submit(Dept dept) {
		if (Func.isEmpty(dept.getParentId())) {
			dept.setParentId(BladeConstant.TOP_PARENT_ID);
			dept.setAncestors("0");
		}
		if (dept.getParentId() > 0) {
			Dept parent = getById(dept.getParentId());
			String ancestors = parent.getAncestors() + StringPool.COMMA + dept.getParentId();
			dept.setAncestors(ancestors);
		}
		dept.setIsDeleted(BladeConstant.DB_NOT_DELETED);

		return update(dept);
	}

	@Override
	public List<DeptVO2> queryDeptByUserId(Long userId) {
		return baseMapper.queryDeptByUserId( userId);
	}


	public List<DeptVO2> queryDeptByUserId() {
		return baseMapper.queryDeptAll();
	}

	/**
	 * @Description 新增或变更父类部门时创建机构
	 * @Author xiafayu
	 * @Date 2020/9/23 10:28
	 * @param dept
	 * @return boolean
	 * @throws
	 **/
	@Transactional(rollbackFor = Exception.class)
	public boolean update(Dept dept){

		boolean status = saveOrUpdate(dept);

		if (dept.getParentId() == 0) {
			boolean orgaFlag;
			Orga orga = orgaService.getById(dept.getId());
			if(orga == null){
				orga = new Orga();
				orga.setId(dept.getId());
				String orgaCode = serialNumberService.generateSerialNumberByModelCode("opc_orga_code");
				orga.setOrgaCard(orgaCode);
				orga.setRegTime(DateUtil.now());
				orga.setAccessTime(DateUtil.now());
				orga.setOrgaStatus("1");
				orga.setStatus(1);
				orga.setOperationType("1");
				orga.setAuthFlag("1");
				orga.setIsDeleted(0);
				orga.setOrgaName(dept.getDeptName());
				orga.setExpSeq(stringRedisTemplate.opsForValue().get(EXP_SQE));
				orgaFlag = orgaService.save(orga);
			}else{
				orga.setOrgaName(dept.getDeptName());
				orgaFlag = orgaService.updateById(orga);
			}

			status = status && orgaFlag;
		}
		 return status;

	}
}
